package com.gwtintegration.smartrfsandbox.server.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.gwtintegration.smartrf.server.DataRequest;
import com.gwtintegration.smartrf.server.DataRequestQuery;
import com.gwtintegration.smartrfsandbox.server.data.UserDataResponse;
import com.gwtintegration.smartrfsandbox.server.entity.UserEntity;

@Repository
public class UserRepository {

	@PersistenceContext
	private EntityManager entityManager;

	public UserDataResponse fetch(DataRequest dataRequest) {
		DataRequestQuery dataRequestQuery = new DataRequestQuery(dataRequest, UserEntity.class, (Session) entityManager.getDelegate());
		return dataRequestQuery.getResult(new UserDataResponse());
	}

	public UserEntity add(UserEntity entity) {
		entityManager.persist(entity);
		return entity;
	}

	public UserEntity update(UserEntity entity) {
		entityManager.merge(entity);
		return entity;
	}

	public void remove(UserEntity entity) {
		entityManager.remove(entity);
	}
}
